﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RayDen.Library.Core.Primitives;
using RayDen.SceneLab.Controllers.FormControllers;

namespace RayDen.SceneLab.UI
{
    public partial class ColorEditForm : Form
    {
        public ColorEditController Controller;

        public ColorEditForm()
        {
            InitializeComponent();
            Controller = new ColorEditController(this);
        }


        private void lnkBaseColor_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                this.Controller.SelectedColor = colorDialog1.Color;
                this.Refresh();
            }
        }


        private void ctlBaseColor_Paint(object sender, PaintEventArgs e)
        {
            using (var sb = new SolidBrush(this.Controller.SelectedColor))
            {
                e.Graphics.FillRectangle(sb, ctlBaseColor.ClientRectangle);
            }
        }

        private void btnConvertToPS_Click(object sender, EventArgs e)
        {
            this.Controller.ConvertedColor = chkReflection.Checked
                ? this.Controller.PointSampledSpectrumIllum.Transform()
                : Controller.PointSampledSpectrumRefl.Transform();
            this.Refresh();
        }

        private void ctlConvertedColor_Paint(object sender, PaintEventArgs e)
        {
            if (this.Controller.ConvertedColor == null)
                return;

            using (var sb = new SolidBrush(this.Controller.ConvertedColor.GetValueOrDefault()))
            {
                e.Graphics.FillRectangle(sb, ctlBaseColor.ClientRectangle);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Controller.ConvertedColor = null;
            this.Controller.ConvertedColor = chkReflection.Checked
                ? this.Controller.RegularSPD_Refl.ToRgb().ToColor()
                : Controller.RegularSPD_illum.ToRgb().ToColor();
            this.Refresh();
        }

        private void openSPDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.Controller.OpenSPDFile(openFileDialog1.FileName);
                this.lnkSpdName.Text = openFileDialog1.FileName;
                this.Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //this.Controller.ConvertedColor =this.Controller.ConvertRegular(SPD_Data.Al_k.Select(i => (float) i).ToArray());
            this.Controller.ConvertedColor = this.Controller.ConvertRegular(this.Controller.SpectralData);
            this.Refresh();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //this.Controller.ConvertedColor =this.Controller.ConvertIrregular(SPD_Data.Al_k.Select(i => (float)i).ToArray());

            this.Controller.ConvertedColor = this.Controller.ConvertIrregular(this.Controller.SpectralData);
            this.Refresh();
        }

        private void btnC3_Click(object sender, EventArgs e)
        {
            this.Controller.ConvertedColor = this.Controller.ConvertSampled(this.Controller.SpectralData, null, chkReflection.Checked);
            this.Refresh();
        }

        private void lnkSpdName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.Controller.OpenSPDFile(openFileDialog1.FileName);
                this.lnkSpdName.Text = openFileDialog1.FileName;
                this.Refresh();
            }
        }
    }
}
